using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
namespace Net35.Xrm.Sdk.Linq
{
	internal sealed class Query<T> : IOrderedQueryable<T>, IQueryable<T>, IEnumerable<T>, IOrderedQueryable, IQueryable, IEnumerable, IEntityQuery
	{
		public string EntityLogicalName
		{
			get;
			private set;
		}
		public Type ElementType
		{
			get
			{
				return typeof(T);
			}
		}
		public IQueryProvider Provider
		{
			get;
			private set;
		}
		public Expression Expression
		{
			get;
			private set;
		}
		public Query(IQueryProvider provider, string entityLogicalName)
		{
			if (provider == null)
			{
				throw new ArgumentNullException("provider");
			}
			if (StringExtensions.IsNullOrWhiteSpace(entityLogicalName))
			{
				throw new ArgumentNullException("entityLogicalName");
			}
			this.Provider = provider;
			this.EntityLogicalName = entityLogicalName;
			this.Expression = Expression.Constant(this);
		}
		public Query(IQueryProvider provider, Expression expression)
		{
			if (provider == null)
			{
				throw new ArgumentNullException("provider");
			}
			if (expression == null)
			{
				throw new ArgumentNullException("expression");
			}
			this.Provider = provider;
			this.Expression = expression;
		}
		public IEnumerator<T> GetEnumerator()
		{
			QueryProvider queryProvider = this.Provider as QueryProvider;
			if (queryProvider != null)
			{
				return queryProvider.GetEnumerator<T>(this.Expression);
			}
			throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The provider '{0}' is not of the expected type '{1}'.", new object[]
			{
				this.Provider,
				typeof(QueryProvider)
			}));
		}
		IEnumerator IEnumerable.GetEnumerator()
		{
			return this.GetEnumerator();
		}
	}
}
